﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HYBYSoft.Company.Models;
using HYBYSoft.Company.WebSite.Helper;
using System.IO;

namespace HYBYSoft.Company.WebSite.Areas.Admin.Controllers
{
    public class LinksController : Controller
    {
        /// <summary>
        /// 数据库实体上下文对象
        /// </summary>
        CompanySiteEntities _dbContext = new CompanySiteEntities();
        //
        // GET: /Admin/Links/

        [Authentication]
        public ActionResult Index(string linkTitle, DateTime? startTime, DateTime? endTime)
        {
            string title = linkTitle ?? string.Empty;
            DateTime start = startTime.HasValue ? startTime.Value : DateTime.MinValue;
            DateTime end = endTime.HasValue ? endTime.Value : DateTime.MaxValue;

            var links = from l in _dbContext.Links
                        where l.Title.Contains(title) && l.CreateTime >= start && l.CreateTime <= end
                        orderby l.CreateTime descending
                        select l;
            return View(links);
        }

        //
        // GET: /Admin/Links/Details/5

        [Authentication]
        public ActionResult Details(int id, string pageId)
        {
            ViewBag.PageId = pageId;
            Links link = _dbContext.Links.FirstOrDefault(l => l.LinkID == id);
            return View(link);
        }

        //
        // GET: /Admin/Links/Create

        [Authentication]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/Links/Create

        [Authentication]
        [HttpPost]
        public ActionResult Create(Links link)
        {
            //获得当前用户信息
            CurrentUserInfo user = SecurityHelper.GetCurrentUserInfo();

            // TODO: Add insert logic here
            link.Creator = user.Name;
            link.LastEditTime = DateTime.Now;
            link.Editor = user.Name;
            link.CreateTime = DateTime.Now;

            link.Icon = SaveAsIcon();
            link.IsShow = int.Parse(Request["rdiShowIndex"]);

            _dbContext.Links.AddObject(link);

            _dbContext.SaveChanges();

            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/Links/Edit/5

        [Authentication]
        public ActionResult Edit(int id, string pageId)
        {
            ViewBag.PageId = pageId;
            Links link = _dbContext.Links.FirstOrDefault(l => l.LinkID == id);
            return View(link);
        }

        //
        // POST: /Admin/Links/Edit/5

        [Authentication]
        [HttpPost]
        public ActionResult Edit(Links links, string pageId)
        {
            // TODO: Add update logic here
            CurrentUserInfo user = SecurityHelper.GetCurrentUserInfo();

            Links link = _dbContext.Links.FirstOrDefault(l => l.LinkID == links.LinkID);
            link.Title = links.Title;
            link.Url = links.Url;
            link.IsShow = int.Parse(Request["rdiShowIndex"]); ;

            string fileName = SaveAsIcon();
            link.Icon = fileName == string.Empty ? link.Icon : fileName;
            link.Editor = user.Name;
            link.LastEditTime = DateTime.Now;

            _dbContext.SaveChanges();

            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/Links/Delete/5

        [Authentication]
        public ActionResult Delete(int id)
        {
            var link = _dbContext.Links.FirstOrDefault(l => l.LinkID == id);
            _dbContext.Links.DeleteObject(link);
            _dbContext.SaveChanges();

            return RedirectToAction("Index");
        }

        //
        // POST: /Admin/Links/Delete/5

        [Authentication]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            // TODO: Add delete logic here

            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/Links/IndexShow/5

        [Authentication]
        public ActionResult IndexShow(int id)
        {
            var link = _dbContext.Links.FirstOrDefault(l => l.LinkID == id);
            if (link.IsShow == 1)
            {
                link.IsShow = 0;
            }
            else
            {
                link.IsShow = 1;
            }
            _dbContext.SaveChanges();

            return RedirectToAction("Index");
        }


        [Authentication]
        public ActionResult Search()
        {
            return RedirectToAction("Index", new { linkTitle = Request["linkTitle"], startTime = Request["startTime"], endTime = Request["endTime"] });
        }


        /// <summary>
        /// 保存图标
        /// </summary>
        /// <returns>图标文件名</returns>
        [NonAction]
        private string SaveAsIcon()
        {
            string fileName = string.Empty;
            foreach (string upload in Request.Files)
            {
                string path = Server.MapPath(ConfigurationHelper.UplodeImagesPath);
                fileName = Guid.NewGuid().ToString() + Path.GetExtension(Request.Files[upload].FileName);
                if (Path.GetExtension(Request.Files[upload].FileName).Length != 0)
                {
                    Request.Files[upload].SaveAs(Path.Combine(path, fileName));
                }
                else
                {
                    fileName = string.Empty;
                }
            }
            return fileName;
        }

        public ActionResult ShowLinks()
        {
            var link = from l in _dbContext.Links
                       where l.IsShow == 1
                       select l;

            //ViewData.Add("links", link);
            return PartialView("ShowLinks", link);
        }

        public ActionResult Short()
        {
            var link = from l in _dbContext.Links
                       where l.IsShow == 1
                       select l;

            //ViewData.Add("links", link);
            return View(link);
        }
        
    }
}
